<template>
	<view class="page-container">
		<view class="container-boby">
			<view class="login-form">
				<!-- 标题 -->
				<view class="login-form-title">用户登录</view>
				<!-- form表单 -->
				<uni-forms :modelValue="loginForm" :rules="rules" ref="refLoginForm">
					<uni-forms-item name="username">
						<uni-easyinput prefixIcon="person" type="text" v-model="loginForm.username"
							placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item name="password">
						<uni-easyinput prefixIcon="locked" type="password" v-model="loginForm.password"
							placeholder="请输入密码" />
					</uni-forms-item>
				</uni-forms>
				<!-- 提交按钮 -->
				<u-button @click="submit" type="primary" shape="circle">登录</u-button>
				<!-- 忘记密码注册 -->
				<view class="login-form-text">
					<text style="color: #007AFF;">忘记密码</text>
					<view style="height: auto;width: 10rpx;">
						<u-line direction="col" color="gray" style="height: 100%;width: 10rpx;"></u-line>
					</view>
					<view>
						还没有账号,
						<text style="color: #007AFF;" @click="handleGoRegister">立即注册</text>
					</view>
				</view>
				<!-- 第三方登录 -->
				<u-divider text="第三方登录"></u-divider>
				<view class="third-login">
					<!--#ifdef MP-WEIXIN-->
					<u-icon name="weixin-circle-fill" color="#5CB529" size="28" @click="wechatMpLogin"></u-icon>
					<!--#endif -->
					<!--#ifdef APP-PLUS-->
					<u-icon name="weixin-circle-fill" color="#5CB529" size="28" @click="wechatAppLogin"></u-icon>
					<!--#endif -->
					<!--#ifdef H5-->
					<u-icon name="weixin-circle-fill" color="#5CB529" size="28" @click="wechatH5Login"></u-icon>
					<!--#endif -->
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {API_URL,WX_APPID,CALL_URL} from '@/config/index.js'
	export default {
		data() {
			return {
				// 登录表单
				loginForm: {
					username: '16666666666',
					password: '12345678a'
				},
				//表单验证规则
				rules: {
					username: {
						rules: [{
								required: true,
								errorMessage: '请输入电话号码'
							},
							{
								pattern: '^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$',
								errorMessage: '请正确输入号码格式'
							}
						]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码'
						}]
					},
				}
			}
		},
		methods: {
			/**
			 * 登录提交
			 * */
			submit() {
				const that = this
				const {
					username,
					password
				} = that.loginForm
				let data = {
					phone: username,
					password
				}
				this.$refs.refLoginForm.validate().then(res => {
					// #ifdef MP-WEIXIN
					uni.login({
						provider: 'weixin',
						success(wxres) {
							data.raw = JSON.stringify(wxres)
							that.toLogin(data)
						}
					})
					// #endif
					// #ifdef H5 || APP-PLUS
					this.toLogin(data)
					// #endif
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			toLogin(datas) {
				uni.request({
					url: 'http://192.168.1.188:8080/m.api',
					data: {
						_gp: 'user',
						_mt: 'login',
						...datas
					},
					method: 'GET',
					success(response) {
						uni.setStorageSync('user',JSON.stringify(response.data.data))
						uni.showToast({
							title:'登录成功',
							icon:'none'
						})
						uni.switchTab({
							url:'/pages/index/index'
						})
					},
					fail(err) {
						uni.showToast({
							title:'登录失败',
							icon:'none'
						})
					}
				})
			},
			/**
			 * 第三方微信登录
			 */
			wechatMpLogin() {
				let that = this
				// #ifdef MP-WEIXIN || APP-PLUS
				uni.login({
					provider: 'weixin',
					success: (wxres) => {
						uni.request({
							url: 'http://192.168.1.188:8080/m.api',
							method: 'GET',
							data: {
								"_gp": "user",
								"_mt": "thirdPartLogin",
								loginType: 1,
								raw: JSON.stringify(wxres)
							},
							success: (res) => {
								let data = res.data
								if (200 === data.errno) {
									let user = res.data.data
									uni.setStorageSync("user", JSON.stringify(user))
									uni.showToast({
										title: '登录成功',
										icon:'none'
									})
								}else{
									uni.showToast({
										icon:'none',
										title:data.errmsg
									})
								}
							},
							fail: (err) => {
								uni.showToast({
									title: '登录失败',
									icon:'none'
								})
							}
						})
					}
				})
				// #endif
			},
			wechatH5Login(){
				const BACKURL = encodeURIComponent(CALL_URL);
				const url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + WX_APPID + "&redirect_uri=" + BACKURL + 
					"&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
				// const urls ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+ WX_APPID +'&redirect_uri='+ BACKURL+
				// '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
				console.log(url);
				window.location.href = url
			},
			wechatAppLogin(){
				uni.login({
					provider:'weixin',
					success:(wxres)=>{
						uni.request({
							url:'http://192.168.1.188:8080/m.api',
							method:'POST',
							data:{
								"_gp":"user",
								"_mt":"thirdPartLogin",
								loginType:2,
								raw:JSON.stringify(wxres)
							},
							header:{
								"Content-Type":"application/x-www-form-urlencoded"
							},
							success:(res)=>{
								let data = res.data
								if(200 === data.errno){
									uni.setStorageSync("user",JSON.stringify(data))
									uni.showToast({
										title:"登录成功",
										icon:'none'
									})
									uni.navigateTo({
										url:"/pages/index/index"
									})
								}else{
									uni.showToast({
										title:"登录失败",
										icon:'none'
									})
								}
							},
							fail:(err)=> {
								uni.showToast({
									title:err,
									icon:'none'
								})
								uni.navigateTo({
									url:"/pages/public/login"
								})
							}
						})
					}
				})
			},	
			handleGoRegister(){
				uni.navigateTo({
					url:'/pages/public/register'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	uni-page-body,
	.page-container,
	.container-boby {
		height: 100%;
	}

	.container-boby {
		background-color: #017F7C;
		display: flex;
		justify-content: center;
		align-items: center;

		.login-form {
			width: 80%;
			height: 80%;
			background-color: white;
			border-radius: 20rpx;
			padding: 0px 40rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;

			.login-form-title {
				padding: 60rpx;
				text-align: center;
				font-size: 50rpx;
			}

			/deep/.is-input-border {
				border-radius: 100rpx;
			}

			.login-form-text {
				width: 100%;
				height: auto;
				display: flex;
				margin: 40rpx 0;
				justify-content: space-evenly;
			}

			.third-login {
				height: 80rpx;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #f8f8f8;
			}
		}
	}
</style>
